package com.geofitti.andoird;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity extends Activity{
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.geofitti_register);
	
		final Button registerButton = (Button) findViewById(R.id.btnRegister);
		final EditText nameText = (EditText) findViewById(R.id.reg_fullname);
		final EditText passText = (EditText) findViewById(R.id.reg_password);
		
		registerButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View arg0){		  	 	
				try {
					String paramString = "?username=" + nameText.getText() + "&password=" + passText.getText();
					JSONObject jsonO = WebCall.doGet("http://icarus.cs.weber.edu/~km06559/geofitti.php/register" + paramString);
					final String status = jsonO.getString("status");
					AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
					builder.setMessage(jsonO.getString("message"))
					       .setCancelable(false)
					       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
									if (status.equals("ok")){
										Intent i = new Intent(getApplicationContext(), GeoFittiActivity.class);
								       	startActivity(i);
								        RegisterActivity.this.finish(); 
								   }
								   else{ //account wasn't registered
									 	
									   dialog.dismiss();
								   }
								
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
				} catch (JSONException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				}
			}
		});			
	}
}
